package com.xinwangai.jingbai.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.xinwangai.jingbai.common.Constants;
import com.xinwangai.jingbai.pojo.DTO.MemberDTO;
import com.xinwangai.jingbai.pojo.VO.MemberVO;
import com.xinwangai.jingbai.service.MemberService;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * 会员控制器
 *
 * @author guo.wenliang
 * @since 2025/6/19 16:27
 */
@RestController
@RequestMapping("/member")
public class MemberController {

    private final MemberService memberService;

    public MemberController(MemberService memberService) {
        this.memberService = memberService;
    }

    @PostMapping("/getMember")
    public String getMember(@RequestBody MemberDTO memberDTO) throws IOException {
        JSONObject jsonObject = new JSONObject();
        Page<MemberVO> members = memberService.getMember(memberDTO);
        PageInfo<MemberVO> page = new PageInfo<>(members);
        jsonObject.put("code", Constants.RESP_RESULT_SUCCESS);
        jsonObject.put("total", members.getTotal());
        jsonObject.put("data", page.getList());

        return jsonObject.toJSONString();
    }

    @PostMapping("/addMember")
    public String addMember(@RequestBody MemberDTO memberDTO) {
        JSONObject jsonObject = new JSONObject();
        try {
            boolean result = memberService.addMember(memberDTO);
            if (result) {
                jsonObject.put("code", Constants.RESP_RESULT_SUCCESS);
                jsonObject.put("msg", "添加成员成功");
            } else {
                jsonObject.put("code", Constants.RESP_RESULT_BUSINESS_ERROR);
                jsonObject.put("msg", "添加成员失败");
            }
        } catch (Exception e) {
            jsonObject.put("code", Constants.RESP_RESULT_SYSTEM_ERROR);
            jsonObject.put("msg", "添加成员异常：" + e.getMessage());
        }
        return jsonObject.toJSONString();
    }

    @PostMapping("/updateMember")
    public String updateMember(@RequestBody MemberDTO memberDTO) {
        JSONObject jsonObject = new JSONObject();
        try {
            boolean result = memberService.updateMember(memberDTO);
            if (result) {
                jsonObject.put("code", Constants.RESP_RESULT_SUCCESS);
                jsonObject.put("msg", "更新成员成功");
            } else {
                jsonObject.put("code", Constants.RESP_RESULT_BUSINESS_ERROR);
                jsonObject.put("msg", "更新成员失败");
            }
        } catch (Exception e) {
            jsonObject.put("code", Constants.RESP_RESULT_SYSTEM_ERROR);
            jsonObject.put("msg", "更新成员异常：" + e.getMessage());
        }
        return jsonObject.toJSONString();
    }

    @PostMapping("/deleteMember")
    public String deleteMember(@RequestBody JSONObject requestBody) {
        JSONObject jsonObject = new JSONObject();
        try {
            Long id = requestBody.getLong("id");
            if (id == null) {
                jsonObject.put("code", Constants.RESP_RESULT_BUSINESS_ERROR);
                jsonObject.put("msg", "成员ID不能为空");
                return jsonObject.toJSONString();
            }

            boolean result = memberService.deleteMember(id);
            if (result) {
                jsonObject.put("code", Constants.RESP_RESULT_SUCCESS);
                jsonObject.put("msg", "删除成员成功");
            } else {
                jsonObject.put("code", Constants.RESP_RESULT_BUSINESS_ERROR);
                jsonObject.put("msg", "删除成员失败");
            }
        } catch (Exception e) {
            jsonObject.put("code", Constants.RESP_RESULT_SYSTEM_ERROR);
            jsonObject.put("msg", "删除成员异常：" + e.getMessage());
        }
        return jsonObject.toJSONString();
    }

    @GetMapping("/getMemberById/{id}")
    public String getMemberById(@PathVariable Long id) {
        JSONObject jsonObject = new JSONObject();
        try {
            MemberVO member = memberService.getMemberById(id);
            if (member != null) {
                jsonObject.put("code", Constants.RESP_RESULT_SUCCESS);
                jsonObject.put("data", member);
                jsonObject.put("msg", "获取成员详情成功");
            } else {
                jsonObject.put("code", Constants.RESP_RESULT_BUSINESS_ERROR);
                jsonObject.put("msg", "成员不存在");
            }
        } catch (Exception e) {
            jsonObject.put("code", Constants.RESP_RESULT_SYSTEM_ERROR);
            jsonObject.put("msg", "获取成员详情异常：" + e.getMessage());
        }
        return jsonObject.toJSONString();
    }
}
